<template>
  <div>
    <!-- 添加或修改OA 请假申请对话框 -->
    <el-drawer
      ref="drawer"
      :title="title"
      direction="rtl"
      :visible.sync="open"
      size="60%"
      append-to-body
      :wrapperClosable="false"
    >
      <el-card
        class="box-card"
        style="width: 96%; margin-left: 20px; min-height: 90vh"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="客户名称" prop="customerId">
                <el-select
                  v-model="form.customerId"
                  disabled
                  filterable
                  placeholder="请选择客户名称"
                  style="width: 100%"
                  @change="getCrmOrderAllNoPage(form.customerId)"
                >
                  <el-option
                    v-for="customer in customerList"
                    :key="customer.customerId"
                    :label="customer.customerName"
                    :value="customer.customerId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商机名称" prop="orderId">
                <el-select
                  v-model="form.orderId"
                  filterable
                  placeholder="请选择商机名称"
                  style="width: 100%"
                  disabled
                >
                  <el-option
                    v-for="order in orderList"
                    :key="order.orderId"
                    :label="order.orderName"
                    :value="order.orderId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请日期" prop="applyTime">
                <el-date-picker
                  style="width: 100%"
                  clearable
                  v-model="form.applyTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择申请日期"
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="费用明细" prop="details" class="details">
                <el-table :data="form.detailList" style="width: 100%">
                  <el-table-column label="报销项目" prop="project">
                    <template slot-scope="scope">
                      <el-input
                        v-model="scope.row.detailsName"
                        placeholder="请输入报销项目"
                        disabled
                      />
                    </template>
                  </el-table-column>
                  <el-table-column label="摘要" prop="detailsSummary">
                    <template slot-scope="scope">
                      <el-input
                        v-model="scope.row.detailsSummary"
                        placeholder="请输入摘要"
                        disabled
                      />
                    </template>
                  </el-table-column>
                  <el-table-column label="金额" prop="detailsAmount">
                    <template slot-scope="scope">
                      <el-input
                        v-model="scope.row.detailsAmount"
                        type="number"
                        placeholder="请输入金额"
                        disabled
                      />
                    </template>
                  </el-table-column>
                </el-table>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合计金额" prop="amount">
                <el-input
                  v-model.number="form.amount"
                  placeholder="请输入合计金额"
                  type="number"
                  disabled
                >
                  <template slot="append">{{
                    digitUppercase(form.amount)
                  }}</template></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="原借款" prop="loan">
                <el-input
                  v-model="form.loan"
                  placeholder="请输入原借款"
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="应退款" prop="refund">
                <el-input
                  v-model="form.refund"
                  placeholder="请输入应退款"
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input
                  v-model="form.remark"
                  type="textarea"
                  placeholder="请输入内容"
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-divider>审批通道</el-divider>
              <el-form-item label="电子签名" prop="signature">
                <div class="box">
                  <VueSignaturePad
                    width="100%"
                    height="250px"
                    ref="signaturePad"
                  />
                  <el-button @click="undo" type="text" class="box-button"
                    >清空签名</el-button
                  >
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="审批意见" prop="message">
                <el-input
                  v-model="form.message"
                  type="textarea"
                  placeholder="请输入审批意见"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24"  v-if="!disabled">
              <el-form-item>
                <el-button type="primary" @click="handleBtn('PASS')"
                  >通过</el-button
                >
                <el-button @click="handleBtn('REJECT')">驳回</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import { getApply, audit } from "@/api/crm/apply";
import { getCrmOrderAllNoPage } from "@/api/crm/order";
import { getCrmCustomerAllNoPage } from "@/api/crm/customer";
export default {
  name: "Approve",
  dicts: ["flow_status", "leave_type"],
  props: {
    /* 业务id */
    value: {
      type: String,
      default: "",
    },

    /* 实例id */
    taskId: {
      type: String,
      default: "",
    },

    /* 是否可以标编辑 */
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 指定跳转节点
      nodeCode: "",
      customerList: [],
      orderList: [],
      rules: {
        // signature: [
        //   {
        //     required: true,
        //     validator: this.validateDetails,
        //     trigger: "change",
        //   },
        // ],
      },
    };
  },
  created() {
    this.reset();
    this.getApply();
  },
  mounted(){
    setTimeout(() => {
      let data = window.localStorage.getItem("签名_" + this.$store.getters.userId)
      if(data){
        this.$refs.signaturePad.fromDataURL(data);
      }
    }, 500)
  },
  methods: {
    validateDetails(rule, value, callback) {
      const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
      // alert(isEmpty)
      if (isEmpty) {
        callback(new Error("电子签名必须填写"));
      } else {
        callback();
      }
    },
    // 撤销操作
    undo() {
      this.$refs.signaturePad.clearSignature();
       console.log(this.$refs.signaturePad)
    },
    // 保存操作
    save() {
      const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
    },
    getCrmCustomerAllNoPage() {
      getCrmCustomerAllNoPage({}).then((res) => {
        this.customerList = res.data;
      });
    },
    async getApply() {
      await getApply(this.value).then((response) => {
        if (!response.data) {
          this.$modal.alertWarning("代办任务不存在");
        }
        this.getCrmCustomerAllNoPage();
        getCrmOrderAllNoPage({ customerId: this.form.customerId }).then(
          (res) => {
            this.orderList = res.data;
          }
        );
        this.form = response.data;
        this.title = this.form.createBy + " 费用申请";
        this.open = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        message: null,
        id: null,
        type: null,
        reason: null,
        startTime: null,
        endTime: null,
        day: null,
        instanceId: null,
        nodeCode: null,
        nodeName: null,
        flowStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 审核通过按钮 */
    handleBtn(skipType) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.taskId = this.taskId;
          this.form.skipType = skipType;
          const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
          this.form.signature = data;
          audit(this.form).then((response) => {
            window.localStorage.setItem("签名_" + this.$store.getters.userId, this.form.signature)
            this.$modal.msgSuccess("办理成功");
            this.open = false;
            this.$emit("refresh");
          });
        }
      });
    },
  },
};
</script>

<style scoped>
::v-deep .el-drawer__header {
  margin-bottom: 20px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .el-textarea.is-disabled .el-textarea__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .details .el-table--medium .el-table__cell {
  padding: 0;
}

::v-deep .details .el-input--medium {
  margin: 10px 10px 10px -10px;
}

.box {
  width: 100%;
  height: 250px;
  border: 1px solid #dcdfe6;
  position: relative;
}

.box-button {
  position: absolute;
  top: 215px;
  left: 93%;
}
</style>